<template>
  <div class="favorite-detail">
    <div class="detail-header">
      <h3>{{ t('favorites.detail.title') }}</h3>
      <el-button 
        :icon="Close" 
        size="small" 
        circle 
        @click="handleClose"
      />
    </div>

    <div class="detail-content">
      <!-- 文件基本信息 -->
      <div class="info-section">
        <h4>{{ t('favorites.detail.basicInfo') }}</h4>
        <div class="file-preview">
          <div class="file-icon">
            <el-icon :size="48">
              <component :is="getFileIcon(file.fileInfo?.mimeType)" />
            </el-icon>
          </div>
          <div class="file-name">{{ file.fileInfo?.originalName }}</div>
        </div>
        
        <div class="info-list">
          <div class="info-item">
            <span class="label">{{ t('favorites.detail.fileSize') }}</span>
            <span class="value">{{ formatFileSize(file.fileInfo?.fileSize) }}</span>
          </div>
          <div class="info-item">
            <span class="label">{{ t('favorites.detail.fileType') }}</span>
            <span class="value">{{ file.fileInfo?.mimeType }}</span>
          </div>
          <div class="info-item">
            <span class="label">{{ t('favorites.detail.uploadTime') }}</span>
            <span class="value">{{ formatDate(file.fileInfo?.createdAt) }}</span>
          </div>
          <div class="info-item">
            <span class="label">{{ t('favorites.detail.modifyTime') }}</span>
            <span class="value">{{ formatDate(file.fileInfo?.updatedAt) }}</span>
          </div>
        </div>
      </div>

      <!-- 收藏信息 -->
      <div class="info-section">
        <h4>{{ t('favorites.detail.favoriteInfo') }}</h4>
        <div class="info-list">
          <div class="info-item">
            <span class="label">收藏分类:</span>
            <el-tag :type="getCategoryType(file.category)">
              {{ getCategoryLabel(file.category) }}
            </el-tag>
          </div>
          <div class="info-item">
            <span class="label">收藏时间:</span>
            <span class="value">{{ formatDate(file.createdAt) }}</span>
          </div>
          <div class="info-item full-width">
            <span class="label">收藏备注:</span>
            <div class="notes-content">
              {{ file.notes || '暂无备注' }}
            </div>
          </div>
        </div>
      </div>

      <!-- 标签信息 -->
      <div class="info-section" v-if="file.tags?.length">
        <h4>标签</h4>
        <div class="tags-container">
          <el-tag 
            v-for="tag in file.tags" 
            :key="tag.id"
            :color="tag.color"
            style="margin-right: 8px; margin-bottom: 8px;"
          >
            {{ tag.name }}
          </el-tag>
        </div>
      </div>

      <!-- 操作按钮 -->
      <div class="action-section">
        <el-button 
          type="primary" 
          :icon="View"
          @click="handlePreview"
          block
        >
          预览文件
        </el-button>
        <el-button 
          type="success" 
          :icon="Download"
          @click="handleDownload"
          block
        >
          下载文件
        </el-button>
        <el-button 
          :icon="Edit"
          @click="handleEditNotes"
          block
        >
          编辑备注
        </el-button>
        <el-button 
          :icon="Share"
          @click="handleShare"
          block
        >
          分享文件
        </el-button>
        <el-button 
          type="danger" 
          :icon="Delete"
          @click="handleRemove"
          block
        >
          取消收藏
        </el-button>
      </div>
    </div>

    <!-- 编辑备注弹窗 -->
    <el-dialog 
      v-model="showEditDialog"
      title="编辑收藏信息"
      width="400px"
    >
      <el-form 
        ref="editFormRef"
        :model="editForm"
        label-width="80px"
      >
        <el-form-item label="分类">
          <el-select v-model="editForm.category" placeholder="选择分类">
            <el-option label="默认" value="default" />
            <el-option label="工作" value="work" />
            <el-option label="学习" value="study" />
            <el-option label="生活" value="life" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="备注">
          <el-input 
            v-model="editForm.notes"
            type="textarea"
            :rows="4"
            placeholder="请输入备注信息"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      
      <template #footer>
        <el-button @click="showEditDialog = false">取消</el-button>
        <el-button 
          type="primary" 
          @click="handleSaveEdit"
          :loading="saving"
        >
          保存
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useI18n } from 'vue-i18n'
import {
  Close, View, Download, Edit, Share, Delete
} from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox, type FormInstance } from 'element-plus'
import { formatFileSize, formatDate, getFileIcon } from '@/utils/file'

// Props
interface Props {
  file: any
}

const props = defineProps<Props>()

// 国际化
const { t } = useI18n()

// Emits
const emit = defineEmits<{
  'close': []
  'update': [file: any, data: any]
}>()

// 响应式数据
const showEditDialog = ref(false)
const saving = ref(false)
const editFormRef = ref<FormInstance>()

const editForm = reactive({
  category: '',
  notes: ''
})

// 事件处理
const handleClose = () => {
  emit('close')
}

const handlePreview = () => {
  // TODO: 实现文件预览
  console.log('Preview file:', props.file)
  ElMessage.info('预览功能开发中...')
}

const handleDownload = () => {
  // TODO: 实现文件下载
  console.log('Download file:', props.file)
  ElMessage.info('下载功能开发中...')
}

const handleEditNotes = () => {
  editForm.category = props.file.category || 'default'
  editForm.notes = props.file.notes || ''
  showEditDialog.value = true
}

const handleShare = () => {
  // TODO: 实现文件分享
  console.log('Share file:', props.file)
  ElMessage.info('分享功能开发中...')
}

const handleRemove = async () => {
  try {
    await ElMessageBox.confirm(
      `确定要取消收藏文件"${props.file.fileInfo?.originalName}"吗？`,
      '取消收藏',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    // TODO: 实现取消收藏
    console.log('Remove favorite:', props.file)
    ElMessage.success('取消收藏成功')
    emit('close')
  } catch (error) {
    // 用户取消
  }
}

const handleSaveEdit = async () => {
  saving.value = true
  try {
    const updateData = {
      category: editForm.category,
      notes: editForm.notes
    }
    
    emit('update', props.file, updateData)
    showEditDialog.value = false
    ElMessage.success('更新成功')
  } catch (error) {
    console.error('Failed to update favorite:', error)
    ElMessage.error('更新失败')
  } finally {
    saving.value = false
  }
}

// 工具函数
const getCategoryType = (category: string) => {
  const typeMap: Record<string, string> = {
    document: 'primary',
    image: 'success',
    video: 'warning',
    audio: 'info',
    archive: 'danger',
    code: 'primary',
    spreadsheet: 'success',
    presentation: 'warning',
    other: '',
    default: ''
  }
  return typeMap[category] || ''
}

const getCategoryLabel = (category: string) => {
  const labelMap: Record<string, string> = {
    document: '文档',
    image: '图片',
    video: '视频',
    audio: '音频',
    archive: '压缩包',
    code: '代码',
    spreadsheet: '表格',
    presentation: '演示文稿',
    other: '其他',
    default: '默认'
  }
  return labelMap[category] || category
}
</script>

<style scoped>
.favorite-detail {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: var(--el-bg-color-page);
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid var(--el-border-color);
}

.detail-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.detail-content {
  flex: 1;
  padding: 16px;
  overflow-y: auto;
}

.info-section {
  margin-bottom: 24px;
}

.info-section h4 {
  margin: 0 0 12px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--el-text-color-primary);
}

.file-preview {
  text-align: center;
  margin-bottom: 16px;
  padding: 16px;
  background: var(--el-bg-color);
  border-radius: 8px;
}

.file-icon {
  margin-bottom: 8px;
  color: var(--el-color-primary);
}

.file-name {
  font-weight: 500;
  word-break: break-all;
}

.info-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.info-item.full-width {
  flex-direction: column;
  align-items: stretch;
}

.label {
  font-size: 12px;
  color: var(--el-text-color-secondary);
  min-width: 60px;
  flex-shrink: 0;
}

.value {
  font-size: 12px;
  color: var(--el-text-color-primary);
  flex: 1;
}

.notes-content {
  margin-top: 4px;
  padding: 8px;
  background: var(--el-bg-color);
  border-radius: 4px;
  font-size: 12px;
  color: var(--el-text-color-regular);
  line-height: 1.4;
  white-space: pre-wrap;
}

.tags-container {
  display: flex;
  flex-wrap: wrap;
}

.action-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 24px;
  padding-top: 16px;
  border-top: 1px solid var(--el-border-color);
}
</style>
